package com.ruoyi.shop.cms.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.cms.domain.ShopMaterialGroup;
import com.ruoyi.shop.cms.mapper.ShopMaterialGroupMapper;
import com.ruoyi.shop.cms.service.IShopMaterialGroupService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 素材分组Service业务层处理
 * 
 * @author xuxiang
 * @date 2022-09-26
 */
@Service
public class ShopMaterialGroupServiceImpl implements IShopMaterialGroupService 
{
    @Autowired
    private ShopMaterialGroupMapper shopMaterialGroupMapper;

    /**
     * 查询素材分组
     * 
     * @param groupId 素材分组主键
     * @return 素材分组
     */
    @Override
    public ShopMaterialGroup selectGroupById(Long groupId)
    {
        return shopMaterialGroupMapper.selectGroupById(groupId);
    }

    /**
     * 查询素材分组列表
     * 
     * @param shopMaterialGroup 素材分组
     * @return 素材分组
     */
    @Override
    public List<ShopMaterialGroup> selectGroupList(ShopMaterialGroup shopMaterialGroup)
    {
        return shopMaterialGroupMapper.selectGroupList(shopMaterialGroup);
    }

    /**
     * 新增素材分组
     * 
     * @param shopMaterialGroup 素材分组
     * @return 结果
     */
    @Override
    public int insertGroup(ShopMaterialGroup shopMaterialGroup)
    {
		shopMaterialGroup.setParentId(new Long(0));
		shopMaterialGroup.setAncestors("0");
        shopMaterialGroup.setCreateTime(DateUtils.getNowDate());
        return shopMaterialGroupMapper.insertGroup(shopMaterialGroup);
    }

    /**
     * 修改素材分组
     * 
     * @param shopMaterialGroup 素材分组
     * @return 结果
     */
    @Override
    public int updateGroup(ShopMaterialGroup shopMaterialGroup)
    {
        shopMaterialGroup.setUpdateTime(DateUtils.getNowDate());
        return shopMaterialGroupMapper.updateGroup(shopMaterialGroup);
    }

    /**
     * 批量删除素材分组
     * 
     * @param groupIds 需要删除的素材分组主键
     * @return 结果
     */
    @Override
    public int deleteByGroupIds(String groupIds)
    {
        return shopMaterialGroupMapper.deleteByGroupIds(Convert.toStrArray(groupIds));
    }

    /**
     * 删除素材分组信息
     * 
     * @param groupId 素材分组主键
     * @return 结果
     */
    @Override
    public int deleteByGroupId(Long groupId)
    {
        return shopMaterialGroupMapper.deleteByGroupId(groupId);
    }
    
	/**
	 * 查询所有素材
	 * 
	 * @return
	 */
	@Override
	public List<ShopMaterialGroup> selectAll() {
		return shopMaterialGroupMapper.selectAll();
	}
	
	  /**
     * 查询分组管理树（排除下级）
     * 
     * @return 所有部门信息
     */
    @Override
    public List<Ztree> selectGroupTreeExcludeChild(ShopMaterialGroup shopMaterialGroup)
    {
        Long excludeId = shopMaterialGroup.getExcludeId();
        List<ShopMaterialGroup> groups = shopMaterialGroupMapper.selectAll();
        if (excludeId.intValue() > 0)
        {
        	groups.removeIf(d -> d.getGroupId().intValue() == excludeId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), excludeId + ""));
        }
        List<Ztree> ztrees = initZtree(groups);
        return ztrees;
    }
    
    
	/**
	 * 对象转分组树
	 *
	 * @param groupList 分组列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<ShopMaterialGroup> groupList) {
		List<Ztree> ztrees = new ArrayList<Ztree>();
		for (ShopMaterialGroup group : groupList) {
			Ztree ztree = new Ztree();
			ztree.setId(group.getGroupId());
			ztree.setpId(group.getParentId());
			ztree.setName(group.getGroupName());
			ztree.setTitle(group.getGroupName());
			ztrees.add(ztree);
		}
		return ztrees;
	}

    
}
